package com.usian.project.controller;


import com.github.pagehelper.PageInfo;
import com.usian.commom.pojo.Area;
import com.usian.commom.pojo.Project;
import com.usian.commom.vo.ResultCommon;
import com.usian.project.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * @TODO:
 * @Author:baimu
 * @Date:2024 10 28 14 25
 **/

@RestController
@RequestMapping("/project")
@Slf4j
@RefreshScope
public class PjojectController {

    final
    ProjectService projectService;

    public PjojectController(ProjectService projectService) {
        this.projectService = projectService;
    }




//    @GetMapping("/findAll")
//    public List<Project> findAll()
//    {
//        return projectService.findAll();
//    }


    @PostMapping("/findAll")
    public ResultCommon findALl(
           @RequestParam(name = "pageNo",defaultValue = "1") Integer pageNo,
           @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize,
           @RequestBody Project project1){

        log.info("pageNo:{}",pageNo);
        log.info("pageSize:{}",pageSize);

        log.info("project:{}",project1.toString());
        PageInfo<Project> pageInfo = projectService.findAll(pageNo, pageSize,project1);

        return ResultCommon.suceessData(pageInfo);

    }

    @PostMapping("/insertProject")
    public ResultCommon insertProject(@RequestBody Project project)
    {
        log.info("project:{}",project.toString());
        Integer i = projectService.insertProject(project);
        if(i>0){
            return ResultCommon.suceess();
        }else{
            return ResultCommon.error("添加失败");
        }


    }


    @DeleteMapping("/deleteProjectById/{id}")
    public ResultCommon deleteProjectById(@PathVariable Integer id)
    {
        Integer i = projectService.deleteProjectById(id);
        if(i>0){
            return ResultCommon.suceess();
        }else{
            return ResultCommon.error("删除失败");
        }

    }


    @Value("${dateFormat}")
    String dateFormat;

    @GetMapping("/testDate")
    public ResultCommon testDate(){
        LocalDateTime now = LocalDateTime.now();
        log.info("LocalDateTime:{}",now);

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(dateFormat);
        String format = dateTimeFormatter.format(now);

        log.info("format:{}",format);
        return ResultCommon.suceessData(format);

    }

    @GetMapping("/findAllArea")
    public List<Area> findAllArea(){
        return projectService.findAllArea();
    }

}
